#include "bsp_adc.h"


void BSP_ADC_init(void)
{
	stc_adc_cfg_t stcAdcCfg;
	DDL_ZERO_STRUCT(stcAdcCfg);
	Sysctrl_SetPeripheralGate(SysctrlPeripheralAdcBgr, TRUE);

	stcAdcCfg.enAdcMode         = AdcSglMode;
	stcAdcCfg.enAdcClkDiv       = AdcMskClkDiv1;	    ///<采样分频1
	stcAdcCfg.enAdcSampCycleSel = AdcMskSampCycle8Clk;	///<采样周期数:8
	stcAdcCfg.enAdcRefVolSel    = AdcMskRefVolSelAVDD;		///<参考电压选择-VCC
	
	stcAdcCfg.enAdcOpBuf		= AdcMskBufEnable; 			///<OP BUF配置-on
	stcAdcCfg.enInRef			= AdcMskInRefDisable;		///<内部参考电压使能-关
	stcAdcCfg.enAdcAlign		= AdcAlignRight;			///<转换结果对齐方式-右

	Adc_Init(&stcAdcCfg);
	
	BSP_ADC_Disable(); /*Save Power!*/
	
}

void BSP_ADC_Enable(void)
{
	Sysctrl_SetPeripheralGate(SysctrlPeripheralAdcBgr, TRUE);
	Adc_Enable();
	Bgr_BgrEnable();
}

void BSP_ADC_Disable(void)
{
	Bgr_BgrDisable();
	Adc_Disable();	 
	M0P_SYSCTRL->PERI_CLKEN_f.ADC = 0x0u;
	Sysctrl_SetPeripheralGate(SysctrlPeripheralAdcBgr, FALSE);
}


/*
************************************************************************
*	函 数 名: ADC_get_value
*	形    参：adc通道.
*	返 回 值: 转换结果
*	说    明: 选择通道：AdcAiTsInput，必须使用Bgr_TempSensorEnable()和Bgr_TempSensorDisable()
************************************************************************
*/
uint16_t BSP_ADC_Read(en_adc_samp_ch_sel_t Channel)
{
	M0P_ADC->CR0_f.SGLMUX = Channel;
	M0P_ADC->ICR_f.SGLIC = 0;
	M0P_ADC->SGLSTART_f.START = 1u;
	while (!M0P_ADC->IFR_f.SGLIF);
	return M0P_ADC->RESULT_f.RESULT;
}

